package com.animal.controller;


import com.animal.auto.JSONAuthentication;
import com.animal.entity.AnAnimal;
import com.animal.entity.AnCollect;
import com.animal.mapper.AnCollectMapper;
import com.animal.response.Result;
import com.animal.service.AnCollectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author animal
 * @since 2022-01-29
 */
@RestController
@RequestMapping("/an-collect")
public class AnCollectController extends JSONAuthentication {

    @Autowired
    AnCollectMapper anCollectMapper;
    @Autowired
    AnCollectService anCollectService;

    @ApiOperation(value = "添加收藏")
    @PostMapping("/add")
    public void addCollect(HttpServletRequest request, HttpServletResponse response, @RequestBody AnCollect anCollect) throws  Exception{
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        anCollect.setCoCreatTime(timestamp);
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_id",anCollect.getUserId());
       if(anCollectMapper.selectList(queryWrapper).size()>0){
           List<AnCollect> anCollect1=anCollectMapper.selectList(queryWrapper);
           for(AnCollect a:anCollect1){
               if(a.getInId()!=null&&anCollect.getInId()!=null){
                   if(a.getInId().equals(anCollect.getInId())){
                       Result error = Result.error().message("已收藏了");
                       this.WriteJSON(request, response, error);
                       return;
                   }
               }else if(a.getAnId()!=null&&anCollect.getAnId()!=null){
                   if(a.getAnId().equals(anCollect.getAnId())){
                       Result error = Result.error().message("已收藏了");
                       this.WriteJSON(request, response, error);
                       return;
                   }
               }
           }
           int i=anCollectMapper.insert(anCollect);
           if(i>0){
               Result ok = Result.ok();
               this.WriteJSON(request, response, ok);
           } else {
               Result error = Result.error();
               this.WriteJSON(request, response, error);
           }
           return;

       }else{
           int i=anCollectMapper.insert(anCollect);
           if(i>0){
               Result ok = Result.ok();
               this.WriteJSON(request, response, ok);
           } else {
               Result error = Result.error();
               this.WriteJSON(request, response, error);
           }
       }
    }

    @ApiOperation(value = "删除收藏")
    @PostMapping("/del")
    public void delAnimal(HttpServletRequest request, HttpServletResponse response, @RequestParam("coId") int coId ) throws Exception{
        int i=anCollectMapper.deleteById(coId);
        if(i>0){
            Result ok = Result.ok();
            this.WriteJSON(request, response, ok);
        } else {
            Result error = Result.error();
            this.WriteJSON(request, response, error);
        }
    }

    @ApiOperation(value = "查询收藏")
    @PostMapping("/look")
    public void lookAnimal(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size)throws  Exception{
        int total=anCollectMapper.selectCount(null);
        List<AnCollect> list =anCollectService.findAll(size,current);
        Result ok = Result.ok().data("collect",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }

    @ApiOperation(value = "通过用户id查询收藏")
    @PostMapping("/lookByUserId")
    public void lookAnimalB(HttpServletRequest request, HttpServletResponse response,@RequestParam("current") int current,@RequestParam("size") int size,@RequestParam("userId") int userId)throws  Exception{
        QueryWrapper<AnCollect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        int total=anCollectMapper.selectCount(wrapper);
        List<AnCollect> list =anCollectService.findByUserId(current,size,userId);
        Result ok = Result.ok().data("collect",list);
        ok.data("total",total);
        this.WriteJSON(request, response, ok);
    }

}

